package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.payconfig.EntityVoucherType;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.request.payconfig.NewVoucherTypeRequest;
import com.ocom.common.request.payconfig.UpdateVoucherTypeRequest;
import com.ocom.common.utils.StringUtils;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoVoucherType;
import com.ocom.payconfig.service.VoucherTypeService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class VoucherTypeServiceImpl extends ServiceImpl<IDaoVoucherType, EntityVoucherType> implements VoucherTypeService {

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public List<EntityVoucherType> selectVoucherTypeList(Long comId, String attributeStr, String consTypeStr) {
        QueryWrapper<EntityVoucherType> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("status",0);
        if (StringUtils.isNotEmpty(attributeStr)){
            String[] arr=attributeStr.split(",");
            queryWrapper.in("attribute",arr);
        }
        if (StringUtils.isNotEmpty(consTypeStr)){
            String[] arr=consTypeStr.split(",");
            queryWrapper.in("cons_type",arr);
        }
        queryWrapper.orderByAsc("attribute","cons_type");
        return this.list(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public Boolean removeVoucherType(Long comId, Long voucherTypeId) {
        UpdateWrapper<EntityVoucherType> updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("status",1);
        updateWrapper.eq("com_id",comId);
        updateWrapper.eq("voucher_type_id",voucherTypeId);
        return this.update(updateWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public EntityVoucherType selectVoucherType(Long comId, NewVoucherTypeRequest newVoucherTypeRequest) {
        QueryWrapper<EntityVoucherType> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("status",0);
        queryWrapper.eq("voucher_type_name",newVoucherTypeRequest.getVoucherTypeName());
        queryWrapper.last(" limit 1");
        return this.getOne(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public Boolean newVoucherType(Long comId, NewVoucherTypeRequest newVoucherTypeRequest) {
        EntityVoucherType entityVoucherType=new EntityVoucherType();
        entityVoucherType.setVoucherTypeName(newVoucherTypeRequest.getVoucherTypeName());
        entityVoucherType.setConsType(newVoucherTypeRequest.getConsType());
        Byte b=Byte.decode(String.valueOf(newVoucherTypeRequest.getAttribute()));
        entityVoucherType.setAttribute(b);
        entityVoucherType.setComId(newVoucherTypeRequest.getComId());
        entityVoucherType.setParValue(newVoucherTypeRequest.getParValue());
        entityVoucherType.setRemark(newVoucherTypeRequest.getRemark());
        entityVoucherType.setStatus(0);
        Date date=new Date();
        entityVoucherType.setCreateTime(date);
        entityVoucherType.setCreateUserid(newVoucherTypeRequest.getUserId());
        entityVoucherType.setUpdateTime(date);
        entityVoucherType.setMaxNumber(newVoucherTypeRequest.getMaxNumber());
        entityVoucherType.setDishes(newVoucherTypeRequest.getDishes());
        return this.save(entityVoucherType);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public EntityVoucherType selectUVoucherType(Long comId, UpdateVoucherTypeRequest updateVoucherTypeRequest) {
        QueryWrapper<EntityVoucherType> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("status",0);
        queryWrapper.ne("voucher_type_id",updateVoucherTypeRequest.getVoucherTypeId());
        queryWrapper.eq("voucher_type_name",updateVoucherTypeRequest.getVoucherTypeName());
        queryWrapper.last(" limit 1");
        return this.getOne(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public Boolean updateVoucherType(Long comId, UpdateVoucherTypeRequest updateVoucherTypeRequest) {
        UpdateWrapper<EntityVoucherType> updateWrapper=new UpdateWrapper<>();
        if (StringUtils.isNotEmpty(updateVoucherTypeRequest.getVoucherTypeName())){
            updateWrapper.set("voucher_type_name",updateVoucherTypeRequest.getVoucherTypeName());
        }
        if (updateVoucherTypeRequest.getParValue() !=null){
            updateWrapper.set("par_value",updateVoucherTypeRequest.getParValue());
        }
        if (StringUtils.isNotEmpty(updateVoucherTypeRequest.getRemark())){
            updateWrapper.set("remark",updateVoucherTypeRequest.getRemark());
        }
        updateWrapper.set("update_time",new Date());
        updateWrapper.set("max_number",updateVoucherTypeRequest.getMaxNumber());
        updateWrapper.set("dishes",updateVoucherTypeRequest.getDishes());


        updateWrapper.eq("voucher_type_id",updateVoucherTypeRequest.getVoucherTypeId());
        updateWrapper.eq("com_id",updateVoucherTypeRequest.getComId());
        return this.update(updateWrapper);
    }
}
